package learn.advance.file;

import javax.xml.transform.Source;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;

class Demo1File {
    public static void Test() {
        String path_separator = File.pathSeparator;
        System.out.println(path_separator);

        char pathSeparatorChar = File.pathSeparatorChar;
        System.out.println(pathSeparatorChar);

        String s = File.separator;
        System.out.println(s);
    }
}


class Demo2File {
    public static void Test() {
        Show1();
        Show2("hhh", "ttt");
        Show3();
    }

    private static void Show1() {
        File parent = new File("c:\\");
        File file = new File(parent, "hello.java");
        System.out.println(file);
    }

    private static void Show2(String parent, String child) {
        File file = new File(parent, child);
        System.out.println(file);
    }

    private static void Show3() {
        File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\a.txt");
        System.out.println(f1);
    }
}

class Demo3File {
    public static void Test() {

        Show1();
    }

    private static void Show1() {
        File f1 = new File("D:\\vm16\\7za.exe");
        long l1 = f1.length();
        System.out.println(l1);
        String name1 = f1.getName();
        System.out.println(name1);
        String path1 = f1.getPath();
        System.out.println(path1);
        System.out.println(f1.toString());

        File f2 = new File("D:\\vm16");
        System.out.println(f2.length());
        String name2 = f2.getName();
        System.out.println(name2);

        System.out.println("================");
        //D:\workplace\jdk_l\src\learn\advance\file\TestFile.java
        File f3 = new File("TestFile.java");
        String absolute_path = f3.getAbsolutePath();
        System.out.println(absolute_path);
    }
}

class Demo4File {
    public static void Test() {
        File f1 = new File("D:\\vm16\\7za.exe");
        if (f1.exists()) {
            System.out.println("isDir:" + f1.isDirectory());
            System.out.println("isFile:" + f1.isFile());
        }

        System.out.println("=======================");
        File f2 = new File("D:\\vm16");
        if (f2.exists()) {
            System.out.println("isDir:" + f2.isDirectory());
            System.out.println("isFile:" + f2.isFile());
        }
    }
}

class Demo5File {
    public static void Test() {
        Show1();
        Show2();
        Show3();
    }

    private static void Show1() {
        File f1 = new File("D:\\workplace\\新建文件夹\\新建文件夹");
        boolean b1 = f1.delete();
        System.out.println("b1:" + b1);
        System.out.println("=================");

        File f2 = new File("D:\\workplace\\新建文件夹\\新建文本文档.txt");
        System.out.println(f2.delete());
    }

    private static void Show2() {
        File f1 = new File("D:\\workplace\\新建文件夹\\test");
        boolean b1 = f1.mkdir();
        System.out.println("b1:" + b1);
    }

    private static void Show3() {
        File f1 = new File("D:\\workplace\\新建文件夹\\test\\a.txt");
        boolean b1 = false;
        try {
            b1 = f1.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("b1:" + b1);
    }
}

class Demo6File{
    public static void Test(){
        Show1();
    }

    private static void Show1(){
        File f1=new File("D:\\workplace\\新建文件夹");
        File[] files=f1.listFiles();
        for(File f:files){
            System.out.println(f);
        }
        System.out.println("==========================");
        File f2=new File("D:\\workplace\\新建文件夹\\untitled");
        File[] files2=f2.listFiles();
        for(File f:files2){
            System.out.println(f);
        }

        System.out.println("=========================");
        String[] arr=f2.list();
        for(String f:arr){
            System.out.println(f);
        }
    }
}


class Demo4Recurison{
    public static void Test(){
        File file=new File("D:\\workplace\\新建文件夹");
        GetAllFile(file);

    }

    private static void GetAllFile(File dir){
        System.out.println(dir);
        File[] files=dir.listFiles();
        for(File f:files){
            if(f.isDirectory()){
                GetAllFile(f);
            }else {
                System.out.println(f);
            }
        }
    }
}


public class TestFile {
    public static void main(String[] args) {
        //Demo1File.Test();
        //Demo2File.Test();
        //Demo3File.Test();
        //Demo4File.Test();
        //Demo5File.Test();
        //Demo6File.Test();
        Demo4Recurison.Test();
    }
}
